package com.piesat.quake.business.device.mqtt.convert;

import com.piesat.quake.business.device.mqtt.param.ParseParam;

import java.time.LocalDateTime;
import java.time.ZoneOffset;

import io.netty.buffer.ByteBuf;

/**
 * 时间戳转换器
 *
 * @author Gersy
 * @date 2023/3/17
 * @since 1.0
 */
public class TimestampConvert implements Convert {
    @Override
    public Object read(ByteBuf byteBuf, ParseParam param) {
        double v = byteBuf.readDoubleLE();
        return Double.valueOf(v * 1000).longValue();
    }

    @Override
    public void write(ByteBuf byteBuf, Object value, ParseParam param) {
        long epochMilli = LocalDateTime.now().atZone(ZoneOffset.systemDefault()).toInstant().toEpochMilli();
        double data = ((Long) epochMilli).doubleValue() / 1000;
        byteBuf.writeDoubleLE(data);
    }
}
